/*
 * GestioneGrafica.java
 *
 * Created on 14 aprile 2007, 16.01
 */

package tlApp;

import fnUtils.IdDes;
import fnUtils.PrintUtils;
import fnUtils.SQLUtils;
import fnUtils.StringUtils;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.awt.geom.*;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;


/**
 *
 * @author  franic
 */

public class GestioneGrafica extends javax.swing.JFrame {
    static Evento evento = new Evento();
    static int idEvento;
    static Line2D.Double lineaEventoPrimo = new Line2D.Double();
    static Set<PerEventi> setEventi = new LinkedHashSet<PerEventi>(100);
    static Dimension dim = new Dimension(0,0);
    static Point orig = new Point(0,0);
    static String selCat, selZone;
    static boolean movimEseguito = false, catSelected = true;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    DefaultListModel listCatMod = new DefaultListModel();
    DefaultListModel listZoneMod = new DefaultListModel();
    String txtRicerca;
    
    /**
     * Creates new form GestioneGrafica
     */
    public GestioneGrafica() {
        initComponents();
    }
    
    public GestioneGrafica(int idEvento, Evento evento, String txtRicerca) {
        this.evento = evento;
        this.idEvento = idEvento;
        this.txtRicerca = txtRicerca;
        initComponents();
        ExecSQL.getConn();
        refreshLista();
        if (dim.width == 0) {
            dim.width = (int) (screenSize.width * 0.8);
            dim.height = (int) (screenSize.height * 0.8);
        }
        if (catSelected) {
            rbtCategorie.setSelected(true);
        } else {
            rbtZone.setSelected(true);
        }
        this.setSize(dim);
        this.setLocation(orig);
        TLPanel.rapporto = 0;
        setEventi.clear();
    }
    
    protected void refreshLista() {
        ResultSet rs;
        
        listCatMod.removeAllElements();
        lstCat.setModel(listCatMod);
        rs = ExecSQL.readTab("CATEGORIA",1);
        try {
            while (rs.next()) {
                IdDes inCat = new IdDes(rs.getInt(1),
                        SQLUtils.fromDB(rs.getString(2)));
                listCatMod.addElement(inCat);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        if (selCat != null && !selCat.equals("")){
            int s = 0;
            int[] ind = new int[(int) Math.ceil(((double)selCat.length()/2))];
            for (int i=0; i<listCatMod.getSize(); i++) {
                int n = ((IdDes)listCatMod.get(i)).getId();
                if (selCat.indexOf(""+n) > -1 ) {
                    ind[s++] = i;
                }
            }
            if (s > 0) {
                lstCat.setSelectedIndices(ind);
            }
        }
        
        listZoneMod.removeAllElements();
        lstZone.setModel(listZoneMod);
        rs = ExecSQL.readTab("ZONA",1);
        try {
            while (rs.next()) {
                IdDes inZone = new IdDes(rs.getInt(1),
                        SQLUtils.fromDB(rs.getString(2)));
                listZoneMod.addElement(inZone);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        if (selZone != null && !selZone.equals("")){
            int s = 0;
            int[] ind = new int[(int) Math.ceil(((double)selZone.length()/2))];
            for (int i=0; i<listZoneMod.getSize(); i++) {
                int n = ((IdDes)listZoneMod.get(i)).getId();
                if (selZone.indexOf(""+n) > -1 ) {
                    ind[s++] = i;
                }
            }
            if (s > 0) {
                lstZone.setSelectedIndices(ind);
            }
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        javax.swing.JButton butStampa;

        buttonGroup1 = new javax.swing.ButtonGroup();
        butStampa = new javax.swing.JButton();
        butCancel = new javax.swing.JButton();
        butRidisegna = new javax.swing.JButton();
        lblEvento = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnlTimeLine = new TLPanel();
        sldRapporto = new javax.swing.JSlider();
        lblAnniTot = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstCat = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstZone = new javax.swing.JList();
        butUp = new javax.swing.JButton();
        butDown = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblMsg = new javax.swing.JLabel();
        rbtCategorie = new javax.swing.JRadioButton();
        rbtZone = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("TimeLine");
        setMinimumSize(new java.awt.Dimension(800, 600));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        butStampa.setText("Stampa");
        butStampa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butStampaActionPerformed(evt);
            }
        });

        butCancel.setText("Exit");
        butCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butCancelActionPerformed(evt);
            }
        });

        butRidisegna.setText("Ridisegna");
        butRidisegna.setToolTipText("Ridisegna la TimeLine");
        butRidisegna.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butRidisegnaActionPerformed(evt);
            }
        });

        lblEvento.setText("evento");

        jScrollPane1.setBorder(null);
        jScrollPane1.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        pnlTimeLine.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlTimeLine.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pnlTimeLineMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout pnlTimeLineLayout = new javax.swing.GroupLayout(pnlTimeLine);
        pnlTimeLine.setLayout(pnlTimeLineLayout);
        pnlTimeLineLayout.setHorizontalGroup(
            pnlTimeLineLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 898, Short.MAX_VALUE)
        );
        pnlTimeLineLayout.setVerticalGroup(
            pnlTimeLineLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 528, Short.MAX_VALUE)
        );
        jScrollPane1.setViewportView(pnlTimeLine);

        sldRapporto.setMaximum(500);
        sldRapporto.setMinimum(120);
        sldRapporto.setValue(310);
        sldRapporto.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldRapportoStateChanged(evt);
            }
        });

        lblAnniTot.setFont(new java.awt.Font("Arial", 0, 12));
        lblAnniTot.setText("anniTot");

        jScrollPane2.setViewportView(lstCat);

        jScrollPane3.setViewportView(lstZone);

        butUp.setText("Up");
        butUp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                butUpMouseExited(evt);
            }
        });
        butUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butUpActionPerformed(evt);
            }
        });

        butDown.setText("Down");
        butDown.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                butDownMouseExited(evt);
            }
        });
        butDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butDownActionPerformed(evt);
            }
        });

        jLabel1.setText("Categorie");

        jLabel2.setText("Zone");

        buttonGroup1.add(rbtCategorie);
        rbtCategorie.setSelected(true);
        rbtCategorie.setText("Ordina per Categorie");
        rbtCategorie.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbtCategorie.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rbtCategorie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtCategorieActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbtZone);
        rbtZone.setText("Ordina per Zone");
        rbtZone.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbtZone.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rbtZone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtZoneActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 737, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addComponent(lblEvento, javax.swing.GroupLayout.PREFERRED_SIZE, 431, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(butUp)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(butRidisegna, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(butDown))))
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(22, 22, 22)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(90, 90, 90)
                                                .addComponent(lblMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(8, 8, 8)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(sldRapporto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addGap(10, 10, 10)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                            .addComponent(rbtCategorie)
                                                            .addComponent(lblAnniTot, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(rbtZone))))
                                                .addGap(22, 22, 22))))
                                    .addComponent(jLabel2))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(butStampa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(butCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {butCancel, butStampa});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane2, jScrollPane3});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {butDown, butRidisegna, butUp});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblEvento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(butDown)
                                        .addGap(9, 9, 9))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(sldRapporto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblAnniTot)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(rbtCategorie, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(lblMsg)
                                            .addComponent(butStampa)
                                            .addComponent(butCancel)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(44, 44, 44)
                                        .addComponent(butRidisegna))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(butUp)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(rbtZone)
                        .addGap(10, 10, 10)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jScrollPane2, jScrollPane3});

        pack();
    }// </editor-fold>//GEN-END:initComponents
        
    private void rbtZoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtZoneActionPerformed
        catSelected=false;
        repaint();
    }//GEN-LAST:event_rbtZoneActionPerformed
    
    private void rbtCategorieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtCategorieActionPerformed
        catSelected=true;
        repaint();
    }//GEN-LAST:event_rbtCategorieActionPerformed
    
    private void butUpMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butUpMouseExited
        if (movimEseguito) {
            movimEseguito = false;
            lstCat.clearSelection();
            lstZone.clearSelection();
        }
    }//GEN-LAST:event_butUpMouseExited
    
    private void butDownMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butDownMouseExited
        if (movimEseguito) {
            movimEseguito = false;
            lstCat.clearSelection();
            lstZone.clearSelection();
        }
    }//GEN-LAST:event_butDownMouseExited
    
    private void butDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butDownActionPerformed
        if (lstCat.getSelectedValues().length +
                lstZone.getSelectedValues().length != 1) {
            lblMsg.setText("Selezionare una sola riga");
        } else {
            movimEseguito = true;
            if (lstCat.getSelectedValues().length == 1) {
                int moveMe = lstCat.getSelectedIndex();
                if (moveMe != listCatMod.getSize()-1) {
                    int numItems = lstCat.getModel().getSize();
                    GestioneLookUp2.swap(moveMe, moveMe+1, listCatMod);
                    lstCat.setModel(listCatMod);
                    lstCat.setSelectedIndex(moveMe+1);
                    lstCat.ensureIndexIsVisible(moveMe+1);
                }
            } else {
                int moveMe = lstZone.getSelectedIndex();
                if (moveMe != listZoneMod.getSize()-1) {
                    int numItems = lstZone.getModel().getSize();
                    GestioneLookUp2.swap(moveMe, moveMe+1, listZoneMod);
                    lstZone.setModel(listZoneMod);
                    lstZone.setSelectedIndex(moveMe+1);
                    lstZone.ensureIndexIsVisible(moveMe+1);
                }
            }
        }
    }//GEN-LAST:event_butDownActionPerformed
    
    private void butUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butUpActionPerformed
        if (lstCat.getSelectedValues().length +
                lstZone.getSelectedValues().length != 1) {
            lblMsg.setText("Selezionare una sola riga");
        } else {
            movimEseguito = true;
            if (lstCat.getSelectedValues().length == 1) {
                int moveMe = lstCat.getSelectedIndex();
                if (moveMe != 0) {
                    int numItems = lstCat.getModel().getSize();
                    GestioneLookUp2.swap(moveMe, moveMe-1, listCatMod);
                    lstCat.setModel(listCatMod);
                    lstCat.setSelectedIndex(moveMe-1);
                    lstCat.ensureIndexIsVisible(moveMe-1);
                }
            } else {
                int moveMe = lstZone.getSelectedIndex();
                if (moveMe != 0) {
                    int numItems = lstZone.getModel().getSize();
                    GestioneLookUp2.swap(moveMe, moveMe-1, listZoneMod);
                    lstZone.setModel(listZoneMod);
                    lstZone.setSelectedIndex(moveMe-1);
                    lstZone.ensureIndexIsVisible(moveMe-1);
                }
            }
        }
    }//GEN-LAST:event_butUpActionPerformed
    
    private void sldRapportoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldRapportoStateChanged
        JSlider source = (JSlider)evt.getSource();
        int rappChg = (int)source.getValue();
        TLPanel.rapporto = (double)rappChg / 100;
        repaint();
    }//GEN-LAST:event_sldRapportoStateChanged
    
    private void pnlTimeLineMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlTimeLineMouseClicked
        Evento eventoInTL;
        Rectangle2D.Double rectEventoInTL;
        Point2D xy = evt.getPoint();
        Iterator iter = setEventi.iterator();
        while (iter.hasNext()) {
            PerEventi pe= (PerEventi) iter.next();
            eventoInTL = pe.getEvento();
            rectEventoInTL = pe.getRect();
            if (rectEventoInTL.contains(xy)) {
                if(evt.getButton() == evt.BUTTON1) { //left
                    if (eventoInTL.id != idEvento) {
                        dim.width = getWidth();
                        dim.height = getHeight();
                        orig = getLocation();
                        new GestioneGrafica(eventoInTL.id, eventoInTL,
                                txtRicerca).setVisible(true);
                        dispose();
                        break;
                    }
                } else {
                    if(evt.getButton() == evt.BUTTON3) { //right
                        new GestioneEvento(eventoInTL.id, eventoInTL,
                                txtRicerca,2).setVisible(true);
                        dispose();
                        break;
                    }
                }
            }
        }
    }//GEN-LAST:event_pnlTimeLineMouseClicked
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        fine();
    }//GEN-LAST:event_formWindowClosing
    
    private void butRidisegnaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butRidisegnaActionPerformed
        selCat = "";
        if (lstCat.getSelectedValues().length != 0) {
            for (int i=0; i<lstCat.getSelectedValues().length; i++) {
                int n = ((IdDes)lstCat.getSelectedValues()[i]).getId();
                selCat += n + ",";
            }
        }
        selCat = StringUtils.left(selCat,selCat.length()-1);
        
        selZone = "";
        if (lstZone.getSelectedValues().length != 0) {
            for (int i=0; i<lstZone.getSelectedValues().length; i++) {
                int n = ((IdDes)lstZone.getSelectedValues()[i]).getId();
                selZone += n + ",";
            }
        }
        selZone = StringUtils.left(selZone,selZone.length()-1);
        
        boolean rcOK = false;
        int numItems = lstCat.getModel().getSize();
        for (int i=0; i<numItems; i++){
            int id = ((IdDes)(listCatMod.get(i))).getId();
            ExecSQL.getConn();
            rcOK = ExecSQL.updateTab("CATEGORIA", id, i+1);
            if (!rcOK) {
                lblMsg.setText("Update CATEGORIA KO");
                break;
            }
        }
        if (rcOK) {
            numItems = lstZone.getModel().getSize();
            for (int i=0; i<numItems; i++){
                int id = ((IdDes)(listZoneMod.get(i))).getId();
                ExecSQL.getConn();
                rcOK = ExecSQL.updateTab("ZONA", id, i+1);
                if (!rcOK) {
                    lblMsg.setText("Update ZONA KO");
                    break;
                }
            }
        }
        repaint();
    }//GEN-LAST:event_butRidisegnaActionPerformed
    
// funziona anche cosi', ma la grafica sparisce quando
// viene nascosta da un'altra finestra
//    void linea() {
//        Graphics2D gg = (Graphics2D) pnlTimeLine.getGraphics();
//        Dimension size = pnlTimeLine.getSize();
//        gg.drawLine(x,offsetY,x,maxY-offsetY);
//     }
    
    private void butStampaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butStampaActionPerformed
        PrintUtils.printComponent(pnlTimeLine);
    }//GEN-LAST:event_butStampaActionPerformed
    
    private void butCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butCancelActionPerformed
        fine();
    }//GEN-LAST:event_butCancelActionPerformed
    
    private void fine() {
        if (ExecSQL.theConn != null)
            try {ExecSQL.theConn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        setVisible(false);
        new GestioneDati(txtRicerca).setVisible(true);
        dispose();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butCancel;
    private javax.swing.JButton butDown;
    private javax.swing.JButton butRidisegna;
    private javax.swing.JButton butUp;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    protected static javax.swing.JLabel lblAnniTot;
    protected static javax.swing.JLabel lblEvento;
    private javax.swing.JLabel lblMsg;
    private javax.swing.JList lstCat;
    private javax.swing.JList lstZone;
    protected static javax.swing.JPanel pnlTimeLine;
    private javax.swing.JRadioButton rbtCategorie;
    private javax.swing.JRadioButton rbtZone;
    private javax.swing.JSlider sldRapporto;
    // End of variables declaration//GEN-END:variables
    
}
